﻿using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using DimensionsHelper.LanguageService.Syntax;

namespace DimensionsHelper.LanguageService.Symbols;

/// <summary>
///     语句块符号，包括Section、Event、Job、GlobalVariables、InputDataSource、
///     OutputDataSource、Logging。
/// </summary>
public class SectionSymbol : AbstractSymbol
{
    public SectionSymbol(SymbolKinds kind, IDeclaration declaration) : base(kind)
    {
        Debug.Assert(kind is SymbolKinds.Section or SymbolKinds.EventSection or SymbolKinds.JobSection
            or SymbolKinds.GlobalVariablesSection or SymbolKinds.InputDataSourceSection
            or SymbolKinds.OutputDataSourceSection or SymbolKinds.LoggingSection);
        Declaration = declaration;
        LocationMap.AddLocation(declaration.NameIdentifier.GetLocation());
        Locals = new DomainSymbolTable(this);
        Name = declaration.Name;
    }


    public override string Name { get; }

    public IDeclaration Declaration { get; }


    /// <summary>
    ///     语句块的本地符号表。
    /// </summary>
    internal SymbolTable Locals { get; }


    /// <summary>
    ///     查找指定命名的本地符号
    /// </summary>
    public ISymbol? FindSymbol(string name)
    {
        return Locals.LookupLocalSymbol(name);
    }


    /// <summary>
    ///     尝试查找指定名称的本地符号
    /// </summary>
    /// <param name="name">符号名</param>
    /// <param name="symbol">查找到的符号</param>
    public bool TryGetSymbolByName(string name, [MaybeNullWhen(false)] out ISymbol symbol)
    {
        return Locals.TryGetSymbol(name, out symbol);
    }
}